package br.com.beautycenter.view;

import br.com.beautycenter.DAO.PersisteDespesas;
import br.com.beautycenter.DAO.PersistePrestacao;
import br.com.beautycenter.DAO.PersisteRecebimentoExtra;
import br.com.beautycenter.DAO.PersisteSangria;
import br.com.beautycenter.DAO.PersisteSessao;
import br.com.beautycenter.DAO.PersisteSuprimento;
import br.com.beautycenter.Funcionalidades.Validacoes;
import br.com.beautycenter.Relatorios.ChamadaRelatorios;
import br.com.beautycenter.bean.RecebimentoExtraCaixa;
import br.com.beautycenter.bean.Sessao;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

public class ControleAtendimento extends javax.swing.JDialog {

    ChamadaRelatorios chamadaRelatorios = new ChamadaRelatorios();
    PersisteSessao persisteSessao = new PersisteSessao();
    PersisteRecebimentoExtra persisteRecebimentoExtra = new PersisteRecebimentoExtra();
    PersisteDespesas persisteDespesas = new PersisteDespesas();
    PersisteSangria persisteSangria = new PersisteSangria();
    PersisteSuprimento persisteSuprimento = new PersisteSuprimento();
    PersistePrestacao persistePrestacao = new PersistePrestacao();
    
    
    public void configuracoesIniciais(){
        jDDataControle.setDate(new Date());
    }
    
    public boolean validarData(){
        //validarDataMaior verifica se a data é maior que a data atual
        if(!Validacoes.validarDataMaior(jDDataControle.getDate())) {
            JOptionPane.showMessageDialog(null, "A data deve ser anterior a data atual");
            return false;
        }
        return true;
    }
    
    /**
     * Verifica se tem algum dado no relatório.
     * @return 
     */
    public boolean validarRelatorio(){
        List<Sessao> listaSessoes = persisteSessao.retornarSessoesPelaData(jDDataControle.getDate());
        List<RecebimentoExtraCaixa> listaRecebimentos = persisteRecebimentoExtra.retornarRecebimentosPelaData(
                jDDataControle.getDate());
        
        if(listaSessoes.isEmpty() && listaRecebimentos.isEmpty()){
            JOptionPane.showMessageDialog(null, "Não há dados para serem exibidos no relatório na data informada.");
            return false;
        }
        
        return true;
    }
    
    public void chamarRelatorio(){
        if(validarData() && validarRelatorio()){
            String observacoes = txtObservacoes.getText();
            chamadaRelatorios.controleAtendimento(
                    jDDataControle.getDate(), observacoes,
                    persisteSuprimento.retornarSomatorioSuprimentoPorData(jDDataControle.getDate()),
                    persisteSessao.totalPagoPorPeriodo(jDDataControle.getDate(), jDDataControle.getDate()),
                    persisteRecebimentoExtra.retornarSomatorioRecebimentoExtraPorData(jDDataControle.getDate()),
                    persisteSangria.retornarSomatorioSangriaPorData(jDDataControle.getDate()),
                    persisteDespesas.retornarSomatorioDespesasPorData(jDDataControle.getDate()),
                    persistePrestacao.retornarSomatorioPrestacoesPagasPorData(jDDataControle.getDate(), false)
            );
        }
    }

    public ControleAtendimento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        configuracoesIniciais();
    }
    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jDDataControle = new com.toedter.calendar.JDateChooser();
        jLabelData = new javax.swing.JLabel();
        jBGerarRelatorio = new javax.swing.JButton();
        jLabelObservacao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtObservacoes = new javax.swing.JTextArea();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ERP BEAUTY CENTER - Controle de Atendimento");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados para o Relatório", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabelData.setText("Data:");

        jBGerarRelatorio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/GerarRelatorio.png"))); // NOI18N
        jBGerarRelatorio.setToolTipText("GERAR RELATÓRIO");
        jBGerarRelatorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGerarRelatorioActionPerformed(evt);
            }
        });

        jLabelObservacao.setText("Observações:");

        txtObservacoes.setColumns(20);
        txtObservacoes.setRows(5);
        jScrollPane1.setViewportView(txtObservacoes);

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Sair.png"))); // NOI18N
        btnSair.setToolTipText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(65, 40));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelObservacao)
                    .addComponent(jLabelData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jBGerarRelatorio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jDDataControle, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jDDataControle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelData, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelObservacao)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBGerarRelatorio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(72, 72, 72))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBGerarRelatorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGerarRelatorioActionPerformed
       chamarRelatorio();
    }//GEN-LAST:event_jBGerarRelatorioActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton jBGerarRelatorio;
    private com.toedter.calendar.JDateChooser jDDataControle;
    private javax.swing.JLabel jLabelData;
    private javax.swing.JLabel jLabelObservacao;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtObservacoes;
    // End of variables declaration//GEN-END:variables
}